from django.contrib import admin
from history.models import History
from django.contrib.auth.models import User

# Register your models here.


class HistoryAdmin(admin.ModelAdmin):
    list_display = ( 'time','history_content')
    search_fields = ('history_content',)
    list_filter = ('time',)
    ordering = ('-time',)
    list_per_page = 10

# 创建自定义的AdminSite 限制访问django原生后台权限
class RestrictedAdminSite(admin.AdminSite):
    def has_permission(self, request):
        allowed_users = ['admin']  # 允许访问后台的管理员名单
        return (
            super().has_permission(request) and 
            request.user.username in allowed_users
        )

admin_site = RestrictedAdminSite(name='restricted_admin')

# 重新注册模型
admin_site.register(User)  # 注册需要管理的模型
admin_site.register(History, HistoryAdmin)
